﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.Remoting.Messaging;
using System.Threading;

namespace chapter4_thread_1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("主线程");
            Console.WriteLine("Thread ID: {0}",
                System.Threading.Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine();
            System.Threading.Thread.Sleep(10);

            // 在父线程中设置线程相关的数据
            CallContext.LogicalSetData("Name", "Alice");
            ThreadPool.QueueUserWorkItem(
                state =>
                {
                    Console.WriteLine("默认允许线程上下文流动。");
                    Console.WriteLine("Name: {0}", CallContext.LogicalGetData("Name"));
                }
                    );

            Console.WriteLine("IsFlowSuppressed: {0}", ExecutionContext.IsFlowSuppressed());
            ExecutionContext.SuppressFlow();
            ThreadPool.QueueUserWorkItem(
                state =>
                {
                    Console.WriteLine("抑制线程上下文流动之后");
                    Console.WriteLine("Name: {0}", CallContext.LogicalGetData("Name"));

                }
                    );


            Console.Write("Hit <Enter> to End.");
            Console.WriteLine();
            Console.ReadLine();
        }

        private static void ComputeBoundOp(object state)
        {
            Console.WriteLine("In ComputeBoundOp, state: {0}", state);
            Console.WriteLine("Thread ID: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine("In ComputeBoundOp End.");
            System.Threading.Thread.Sleep(8);
        }
    }
}
